using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Angel.Daos.IManager;
using Angel.Entities;
namespace Angel.Daos.Manager
{
    public class UserManager:IUserManager
    {
        AngelDataContext ctx = new AngelDataContext();
        Group_UserManager user_groupmanager = new Group_UserManager();
        GroupManager groupManager = new GroupManager();
        
        public User GetById(int id)
        {
            return ctx.Users.OrderBy( u => u.UserName).SingleOrDefault(u => u.UserID == id);
        }

        public void Add(User user)
        {
            ctx.Users.InsertOnSubmit(user);
            ctx.SubmitChanges();
        }

        public void Delete(User user)
        {
            ctx.Users.DeleteOnSubmit(user);
            ctx.SubmitChanges();
        }

        public IEnumerable<User> GetAll()
        {
            return ctx.Users.OrderBy(u => u.UserName);
        }

        public void Delete(long userId)
        {
            var gUser = ctx.Group_Users.Where(u => u.UserID== userId).ToList();
            ctx.Group_Users.DeleteAllOnSubmit(gUser);
            User user = ctx.Users.Single(u => u.UserID == userId);         
            ctx.Users.DeleteOnSubmit(user);
            ctx.SubmitChanges();   
        }

        public void Save(User user) 
        {
            ctx.SubmitChanges();

        }

        public IEnumerable<User> Find(string userNameContain, string fullNameContain, string emailContain, int groupID)
        {
            List<User> list = GetAll().ToList();
            List<User> listFilt = new List<User>();
            if (userNameContain == null) userNameContain = "";
            if (fullNameContain == null) fullNameContain = "";
            if (emailContain == null) emailContain = "";
            foreach (User us in list)
            {
                if (((us.UserName).ToLower()).Contains(userNameContain.ToLower()) && ((us.FullName).ToLower()).Contains(fullNameContain.ToLower()) && ((us.Email).ToLower()).Contains(emailContain.ToLower()))
                {
                    try
                    {
                        if (groupID == 0)
                            listFilt.Add(us);
                        else
                        {
                            foreach (var g in user_groupmanager.GetGroupByUserID(us.UserID).ToList())
                            {
                                if (g.GroupID == groupManager.GetByID(groupID).GroupID)
                                {
                                    listFilt.Add(us);
                                    break;
                                }
                            }

                        }
                    }
                    catch(Exception)
                    {
                        listFilt.Add(us);
                    }
                    
                }
            }
            return listFilt;
        }
    }
}
